package com.kefu.controller;

import com.kefu.dto.*;
import com.kefu.service.SessionService;
import com.kefu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private SessionService sessionService;

    /**
     * 用户登录(支持匿名)
     */
    @PostMapping("/login")
    public ResultDTO<Map<String, Object>> login(@RequestBody UserLoginRequest request) {
        try {
            Map<String, Object> loginInfo = userService.login(request);
            return ResultDTO.success(loginInfo);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

    @PostMapping("/anonymous/login")
    public ResultDTO<Map<String, Object>> anonymousLogin() {
        try {
            Map<String, Object> loginInfo = userService.login(null);
            return ResultDTO.success(loginInfo);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }



    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public ResultDTO<UserDTO> getUserInfo(@RequestParam String userId) {
        try {
            UserDTO user = userService.getUserById(userId);
            return ResultDTO.success(user);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 创建新会话
     */
    @PostMapping("/session")
    public ResultDTO<SessionDTO> createSession(@RequestParam String userId) {
        try {
            SessionDTO session = sessionService.createSession(userId);
            return ResultDTO.success(session);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 获取用户会话列表
     */
    @GetMapping("/sessions")
    public ResultDTO<List<SessionDTO>> getUserSessions(@RequestParam String userId) {
        try {
            List<SessionDTO> sessions = sessionService.getUserSessions(userId);
            return ResultDTO.success(sessions);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 获取会话消息
     */
    @GetMapping("/messages")
    public ResultDTO<Map<String, Object>> getMessages(@RequestParam String sessionId, @RequestParam String userId) {
        try {
            Map<String, Object> messages = sessionService.getSessionMessages(sessionId, userId);
            return ResultDTO.success(messages);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 发送消息
     */
    @PostMapping("/message")
    public ResultDTO<Map<String, Object>> sendMessage(
            @RequestParam String sessionId,
            @RequestParam String userId,
            @RequestParam String content,
            @RequestParam(defaultValue = "1") int messageType) {
        try {
            // 发送者类型：1表示用户
            Object message = sessionService.sendMessage(sessionId, userId, 1, content, messageType);
            Map<String, Object> result = new HashMap<>();
            result.put("message", message);
            return ResultDTO.success(result);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 结束会话
     */
    @PostMapping("/end-session")
    public ResultDTO<Boolean> endSession(@RequestParam String sessionId, @RequestParam String userId) {
        try {
            // 操作类型：1表示用户
            boolean success = sessionService.endSession(sessionId, userId, 1);
            return ResultDTO.success(success);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }
}
